#include "myrectangle.h"
#include <QPainter>
#include <QtMath>
#include <QGraphicsSceneMouseEvent>

MyRectangle::MyRectangle()
{
    this->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
}

QRectF MyRectangle::boundingRect() const
{
    qreal l, b, w, h;
    QPointF cenPt((mspt.x() + mept.x()) / 2.0, (mspt.y() + mept.y()) / 2.0);
    w = qAbs(mept.x() - mspt.x());
    h = qAbs(mept.y() - mspt.y());
    l = cenPt.x() - w / 2;
    b = cenPt.y() - h / 2;
    return QRectF(l, b, w, h).adjusted(-6, -6, 6, 6);
}

void MyRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->drawRect(QRectF(mspt, mept));
    if (this->isSelected()) // 当图元被选中后，需要显示4个小的特征方框
    {
        QPen pen(Qt::DashLine);
        painter->setPen(pen);
        DrawSnapRect(painter, mspt);
        DrawSnapRect(painter, mept);
        DrawSnapRect(painter, QPointF(mspt.x(), mept.y()));
        DrawSnapRect(painter, QPointF(mept.x(), mspt.y()));
    }
}

void MyRectangle::DrawSnapRect(QPainter *painter, QPointF cpt)
{
    QBrush brush(Qt::blue); // 定义成蓝色
    painter->setBrush(brush); // 带笔刷的填充
    painter->drawRect(cpt.x() - 5, cpt.y() - 5, 10, 10); // 方框宽度为10
}

bool MyRectangle::isClickOnBorder(const QPointF &localPos) const
{
    QRectF rect(mspt, mept);
    const qreal borderWidth = 5; // 边缘的宽度，用于捕捉
    return (localPos.x() <= rect.left() + borderWidth || localPos.x() >= rect.right() - borderWidth ||
            localPos.y() <= rect.top() + borderWidth || localPos.y() >= rect.bottom() - borderWidth);
}

int RecChoice = 0;
void MyRectangle::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton) {
        RecChoice = 0;
        QPointF localPos = mapFromScene(event->scenePos());

        if (isClickOnBorder(localPos)) {
            // 检查是否点击在边缘
            QPointF corners[] = { mspt, mept, QPointF(mspt.x(), mept.y()), QPointF(mept.x(), mspt.y()) };

            for (int i = 0; i < 4; ++i) {
                qreal dist = sqrt(pow((corners[i].x() - event->scenePos().x()), 2) + pow((corners[i].y() - event->scenePos().y()), 2));
                if (dist < 5) {
                    RecChoice = i + 1; // 捕捉到矩形的角点，1-4对应四个角
                    return;
                }
            }
        } else {
            // 点击在内部，不处理事件，以便可以在其上绘制其他图形
            event->ignore();
            return;
        }
        QGraphicsItem::mousePressEvent(event);
    }
}

void MyRectangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (RecChoice >= 1 && RecChoice <= 4) {
        // 根据选择的角点，更新对应的对角点
        if (RecChoice == 1) mspt = event->scenePos();
        if (RecChoice == 2) mept = event->scenePos();
        if (RecChoice == 3) { // 左上角和右下角
            mept.setX(mept.x() + (event->scenePos().x() - mspt.x()));
            mept.setY(mept.y() + (event->scenePos().y() - mspt.y()));
            mspt = event->scenePos();
        }
        if (RecChoice == 4) { // 右上角和左下角
            mspt.setX(mspt.x() + (event->scenePos().x() - mept.x()));
            mspt.setY(mspt.y() + (event->scenePos().y() - mept.y()));
            mept = event->scenePos();
        }
    } else {
        // 如果没有捕捉到角点，则移动整个矩形
        QPointF delta = event->scenePos() - event->lastScenePos();
        mspt += delta;
        mept += delta;
    }
    this->update();
}

QPainterPath MyRectangle::shape() const
{
    QPainterPath path;
    path.addRect(QRectF(mspt, mept));
    return path;
}
